home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Objects / xxobject.c < prev   
Encoding:
C/C++ Source or Header  |  2000-08-03  |  2.4 KB  |  117 lines

  1. /* Use this file as a template to start implementing a new object type.
  2.    If your objects will be called foobar, start by copying this file to
  3.    foobarobject.c, changing all occurrences of xx to foobar and all
  4.    occurrences of Xx by Foobar.  You will probably want to delete all
  5.    references to 'x_attr' and add your own types of attributes
  6.    instead.  Maybe you want to name your local variables other than
  7.    'xp'.  If your object type is needed in other files, you'll have to
  8.    create a file "foobarobject.h"; see intobject.h for an example. */
  9.  
  10.  
  11. /* Xx objects */
  12.  
  13. #include "Python.h"
  14.  
  15. typedef struct {
  16.     PyObject_HEAD
  17.     PyObject    *x_attr;    /* Attributes dictionary */
  18. } xxobject;
  19.  
  20. staticforward PyTypeObject Xxtype;
  21.  
  22. #define is_xxobject(v)        ((v)->ob_type == &Xxtype)
  23.  
  24. static xxobject *
  25. newxxobject(arg)
  26.     PyObject *arg;
  27. {
  28.     xxobject *xp;
  29.     xp = PyObject_NEW(xxobject, &Xxtype);
  30.     if (xp == NULL)
  31.         return NULL;
  32.     xp->x_attr = NULL;
  33.     return xp;
  34. }
  35.  
  36. /* Xx methods */
  37.  
  38. static void
  39. xx_dealloc(xp)
  40.     xxobject *xp;
  41. {
  42.     Py_XDECREF(xp->x_attr);
  43.     PyObject_DEL(xp);
  44. }
  45.  
  46. static PyObject *
  47. xx_demo(self, args)
  48.     xxobject *self;
  49.     PyObject *args;
  50. {
  51.     if (!PyArg_NoArgs(args))
  52.         return NULL;
  53.     Py_INCREF(Py_None);
  54.     return Py_None;
  55. }
  56.  
  57. static PyMethodDef xx_methods[] = {
  58.     {"demo",    (PyCFunction)xx_demo},
  59.     {NULL,        NULL}        /* sentinel */
  60. };
  61.  
  62. static PyObject *
  63. xx_getattr(xp, name)
  64.     xxobject *xp;
  65.     char *name;
  66. {
  67.     if (xp->x_attr != NULL) {
  68.         PyObject *v = PyDict_GetItemString(xp->x_attr, name);
  69.         if (v != NULL) {
  70.             Py_INCREF(v);
  71.             return v;
  72.         }
  73.     }
  74.     return Py_FindMethod(xx_methods, (PyObject *)xp, name);
  75. }
  76.  
  77. static int
  78. xx_setattr(xp, name, v)
  79.     xxobject *xp;
  80.     char *name;
  81.     PyObject *v;
  82. {
  83.     if (xp->x_attr == NULL) {
  84.         xp->x_attr = PyDict_New();
  85.         if (xp->x_attr == NULL)
  86.             return -1;
  87.     }
  88.     if (v == NULL) {
  89.         int rv = PyDict_DelItemString(xp->x_attr, name);
  90.         if (rv < 0)
  91.             PyErr_SetString(PyExc_AttributeError,
  92.                     "delete non-existing xx attribute");
  93.         return rv;
  94.     }
  95.     else
  96.         return PyDict_SetItemString(xp->x_attr, name, v);
  97. }
  98.  
  99. static PyTypeObject Xxtype = {
  100.     PyObject_HEAD_INIT(&PyType_Type)
  101.     0,            /*ob_size*/
  102.     "xx",            /*tp_name*/
  103.     sizeof(xxobject),    /*tp_basicsize*/
  104.     0,            /*tp_itemsize*/
  105.     /* methods */
  106.     (destructor)xx_dealloc, /*tp_dealloc*/
  107.     0,            /*tp_print*/
  108.     (getattrfunc)xx_getattr, /*tp_getattr*/
  109.     (setattrfunc)xx_setattr, /*tp_setattr*/
  110.     0,            /*tp_compare*/
  111.     0,            /*tp_repr*/
  112.     0,            /*tp_as_number*/
  113.     0,            /*tp_as_sequence*/
  114.     0,            /*tp_as_mapping*/
  115.     0,            /*tp_hash*/
  116. };
  117.